package com.study.demo;

import java.util.List;

import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.task.service.impl.persistence.entity.TaskEntityManager;

public class DeleteTaskCmd implements Command<Void> {
	
	private String processInstanceId;
	
	public DeleteTaskCmd(String processInstanceId) {
		this.processInstanceId = processInstanceId;
	}


	

	public Void execute(CommandContext commandContext) {
		TaskEntityManager taskEntityManager = org.flowable.task.service.impl.util.CommandContextUtil.getTaskEntityManager(commandContext);
		ExecutionEntityManager executionEntityManager = org.flowable.engine.impl.util.CommandContextUtil.getExecutionEntityManager(commandContext);
		List<ExecutionEntity> executionEntities = executionEntityManager.findChildExecutionsByProcessInstanceId(processInstanceId);
		executionEntities.forEach(executionEntitiy -> taskEntityManager.deleteTasksByExecutionId(executionEntitiy.getId()));
		return null;
	}

}
